package cn.dengta.context.web;

import java.util.HashMap;
import java.util.Map;

import cn.dengta.common.context.Env;
import cn.dengta.context.model.WesKit;
import cn.dengta.webapp.base.util.ConvertFilterUtil;
import com.alibaba.fastjson2.filter.ValueFilter;

/**
 * 默认头像处理
 */
public class GenderDefWesKitFilter implements ValueFilter {

	/** 当前分包 */
	private WesKit wesKit;

	public GenderDefWesKitFilter(WesKit wesKit) {
		this.wesKit = wesKit;
	}

	public static Map<WesKit, GenderDefWesKitFilter> filterMap = new HashMap<>();

	static {
		for (WesKit kit : WesKit.values()) {
			filterMap.put(kit, new GenderDefWesKitFilter(kit));
		}
	}

	@Override
	public Object apply(Object source, String name, Object value) {
		if (value instanceof String && "avatar".equals(name)) {
			// 检查是否以http或https开头
			value = ConvertFilterUtil.convertGenderUrl(this.wesKit, value.toString());
		}
		if (Env.inProduction() && value instanceof String) {
			return ConvertFilterUtil.convertDomainUrl(value.toString());
		}
		return value;
	}

}
